<template>
    <div class="upload-container">
        <el-dialog v-model="dialogVisible" class="dialog-fileupload">
            <el-upload
                class="editor-slide-upload"
                :class="[{'show-picture-card': disabled }]"
                :action="FileUploadUrl"
                :multiple="false"
                :accept="acceptList"
                :file-list="fileList"
                :show-file-list="showFileList"
                list-type="text"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
                :before-upload="beforeUpload"
                :on-change="handleChange"
                >
                <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <el-button @click="cancle()">取 消</el-button>
            <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </el-dialog>
    </div>
</template>
<script>
// 文件上传测试地址 http://123.206.189.55:28810/cos/upload
// 文件上传的正式服地址　http://dfs.upload1.jzexueyun.com/cos/upload
// const VIDEOM_MAXSIZE = 50; //广告视频的最大50M
const videoMax = 50 ;// 50m大小
export default {
  // name: 'editorSlideUpload',
    props: {
        color: {
            type: String,
            default: '#20a0ff'
        }
    },
    data() {
        return {
            dialogVisible: false,
            listObj: {},
            fileList: [],
            FileUploadUrl: process.env.UPLOAD_URL,
            acceptList: '.mp4',
            showFileList: true
        }
    },
    computed:{
        disabled() {
        return this.fileList.length < 5 ? false : true;
        }
    },
    methods: {
        cancle() {
            this.dialogVisible = false;
            this.fileList = [];
            this.listObj = {};
        },
        checkAllSuccess() {
            // console.log(this.listObj)
            return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
        },
        handleSubmit() {
            const arr = Object.keys(this.listObj).map(v => this.listObj[v])
            if (!this.checkAllSuccess()) {
                this.$message('正在上传，请稍后再试！')
                return
            }
            this.$emit('videoSuccess', arr)
            this.listObj = {}
            this.fileList = []
            this.dialogVisible = false
        },
        handleSuccess(response, file) {
            const uid = file.uid;
            const objKeyArr = Object.keys(this.listObj);
            for (let i = 0, len = objKeyArr.length; i < len; i++) {
                if (this.listObj[objKeyArr[i]].uid === uid) {
                    if(response.content){
                        this.listObj[objKeyArr[i]].url = response.content.accessUrl;
                        this.listObj[objKeyArr[i]].hasSuccess = true
                    }
                    return
                }
            }
            
        },
        handleRemove(file) {
            if (file) {
                const uid = file.uid
                const objKeyArr = Object.keys(this.listObj)
                for (let i = 0, len = objKeyArr.length; i < len; i++) {
                    if (this.listObj[objKeyArr[i]].uid === uid) {
                        delete this.listObj[objKeyArr[i]]
                        this.fileList.splice(i,1);
                        return
                    }
                }
            }
        },
    // 获取文件的大小
        _getFileSize(size) {			
            return (size/1048576).toFixed(3);
        },
        beforeUpload(file) {
            var fileList = this.fileList;
            if (this._getFileSize(file.size) > videoMax) {
                this.$message.error(`"${file.name}" 该视频过大,请上传小于50M的视频`);
                // this.showFileList = false;
                return false;
            }

            const isVideo = (file.type === 'video/mp4');
            if (!isVideo) {
                this.$message.error('上传视屏支持mp4格式!');
                return false;
            }

            const _self = this
            const _URL = window.URL || window.webkitURL
            const fileName = file.uid
            this.listObj[fileName] = {}
      
            return new Promise((resolve, reject) => {             
                _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
                resolve(true)
            })
        },
        showDialog() {
            this.dialogVisible = true;
        },
        handleChange(file, fileList) {
            // ahthor shwj fileList 删除文件大于50的
            if(fileList.length){
                 fileList = fileList.filter((item) => {
                    return (this._getFileSize(item.size) < videoMax)&&item.raw.type == 'video/mp4';
                });
            }
            // console.log(fileList);
            this.showFileList = true;
            this.fileList = fileList.slice(-5);
        }   
    }
}
</script>

<style rel="stylesheet/scss" lang="scss">
    .upload-container {
        .editor-slide-upload {
            margin-bottom: 20px;
        }
        .dialog-fileupload {
            .el-dialog--small {
                width: 300px;
            }
        }
        .show-picture-card {
            .el-button--primary {
                display: none;
            }
        }
    }
</style>
